package jmind.core.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * pdf工具类
 * https://www.atatech.org/articles/85498?spm=ata.13269325.0.0.37c449fa2Qs2aQ
 * https://blog.csdn.net/TOP__ONE/article/details/65442390
 *
 * iText 官网：http://itextpdf.com/
 * iText 开发文档： http://developers.itextpdf.com/developers-home
 * @author weibo
 * @date 2019/11/12
 */
public class Template2Pdf {

    public static void main(String[] args) throws IOException, DocumentException {
        Map<String,String>  map=new HashMap<>();
        map.put("fill_1","企业名称");
        map.put("fill_2","企业地址");
        map.put("fill_3","韦搏谢");
        map.put("Email","weib@163.com");
        map.put("fill_5","13710321837");
        String path="/Users/weibo/Downloads/" ;
        jmind.core.pdf.Template2Pdf.createByPdfTemplate(map,path+"清洁版.pdf",path+"out2.pdf");

    }
    /**
     * 根据PDF模板生成新的PDF
     * @param map  模板表单域的属性和值
     * @param sourceFile  模板文件
     * @param targetFile  新的pdf文件
     * @return
     */
    public static File createByPdfTemplate(Map<String,String> map, String sourceFile, String targetFile) throws IOException, DocumentException {
        return createByPdfTemplate(map, sourceFile, targetFile,ChineseFont.BSAE_FONT);
    }
    public static File createByPdfTemplate(Map<String,String> map, String sourceFile, String targetFile,BaseFont substitutionFont) throws IOException, DocumentException {
        File file=new File(targetFile);
        PdfReader reader=null;
        FileOutputStream out=null;
        try {
            // 输出流
             out = new FileOutputStream(file);
            // 读取pdf模板
            reader = new PdfReader(sourceFile);
            ByteArrayOutputStream  bos = new ByteArrayOutputStream();
            PdfStamper stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            form.addSubstitutionFont(substitutionFont);
            Map<String, AcroFields.Item> fields = form.getFields();
            map.forEach((k,v)->{
                if(fields.containsKey(k)){
                    try {
                        form.setField(k,v);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (DocumentException e) {
                        e.printStackTrace();
                    }
                }
            });
//            fields.forEach((k,v)->{
//                String val=map.get(k);
//                if(val!=null){
//                    try {
//                        form.setField(k,val);
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    } catch (DocumentException e) {
//                        e.printStackTrace();
//                    }
//
//                }
//            });
            // 如果为false那么生成的PDF文件还能编辑，一定要设为true
            stamper.setFormFlattening(true);
            stamper.close();
            PdfImportedPage importPage ;
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            ///循环是处理成品只显示一页的问题
            PdfReader pdfReader = new PdfReader(bos.toByteArray());
            for (int i=1;i<=reader.getNumberOfPages();i++){
                importPage = copy.getImportedPage(pdfReader, i);
                copy.addPage(importPage);
            }
            copy.close();
            doc.close();
            bos.close();
            pdfReader.close();
        }finally {
            try {
                reader.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return  file ;

    }




}